package com.demo.web.pc;

import com.demo.common.Constants;
import com.demo.dao.ApplySendMapper;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.*;
import com.demo.entity.ApplySend;
import com.demo.entity.LadingBill;
import com.demo.service.ILadingBillService;
import com.demo.service.IOfferService;
import com.demo.service.IOrderService;
import com.demo.service.IStoreService;
import com.demo.utils.JavaSmsApi;
import com.demo.vo.LadingBillVo;
import com.demo.entity.SalePerson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/ladingBill")
public class LadingBillPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ILadingBillService service;
    @Autowired
    private IOrderService orderervice;
    @Autowired
    private IOfferService offerService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private ApplySendMapper asService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(LadingBillVo arg, HttpSession session) {

        SalePerson user = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (user == null) {
            return new BaseResult(false, "请登录", 401);
        }
        String code = (String) session.getAttribute(Constants.PHONE_CODE);
//        logger.debug(code+"====="+arg.getCode());
        if (!code.equals(arg.getCode())) {
            return new BaseResult(false, "短信验证码不正确", 403);
        }
        arg.setUserId(String.valueOf(user.getId()));
//        arg.setNo(Constants.generateServiceNO("T"));//提单号
        arg.setApplierType("1"); //销售员申请
        arg.setCheckStatus("0");
        Object service_res = service.create(arg, false);
        if (service_res instanceof String) {
            return new BaseResult(false, service_res.toString(), 403);
        }
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/code", method = RequestMethod.GET)
    public BaseResult<Object> code(LadingBillVo arg, HttpSession session) {

        SalePerson user = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (user == null) {
            return new BaseResult(false, "请登录", 401);
        }

        ApplySend lb = asService.selectBySaleId(user.getId().toString());
        if (!lb.getPhone().equals(arg.getPhone())) {
            return new BaseResult(false, "请使用发货手机号发送接收短信", 403);
        }
        if ("15029351330".equals(arg.getPhone())) { // for test
            session.setAttribute(Constants.PHONE_CODE, "211064");
        } else {
            String capText = Constants.generateSmsCode();
            session.setAttribute(Constants.PHONE_CODE, capText);
            try {
                return new BaseResult<Object>(true, JavaSmsApi.sender(capText, arg.getPhone()));
            } catch (IOException e) {
                e.printStackTrace();
                logger.info("发送短信验证码异常: " + capText);
            }
        }

        return new BaseResult(true, "success", 200);

    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(LadingBillVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new ListResult(false, "未登录!", 401);
        }
        if ("true".equals(arg.getUserId())) { //我创建的提单
            arg.setUserId(String.valueOf(sp.getId()));
        } else {
            arg.setUserId(null);
        }
        arg.setApplierType("1"); //提单类型
        List<LadingBill> list = service.queryByPage(arg);
        int total = service.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
